package com.sf.web.controller;

import com.sf.domain.Department;
import com.sf.qo.QueryObject;
import com.sf.service.IDepartmentService;
import com.sf.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * /departmnet/list 请求
     * currentPage  当前页
     */
    @RequestMapping("/department/list")
    public String list(QueryObject queryObject, Model model){
        PageResult result = departmentService.query(queryObject);

        // 把数据放到model 中
        model.addAttribute("pageResult",result);
        // 跳转界面
        return "department/list";
    }

    @RequestMapping("/department/input")
    public String input(Long id , Model model){
        if(id !=null){
            Department department =departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }

    @RequestMapping("/department/saveOrUpdate")
    public String saveOrUpdate(Department department){
        departmentService.saveOrUpdate(department);
        return "redirect:/department/list";
    }

    @RequestMapping("/department/delete")
    public String delete(Long id){
        departmentService.delete(id);
        return "redirect:/department/list";
    }
}
